package com.crm.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.crm.entity.SupplierInvoice;
import com.crm.util.ApplicationContext;

public class SupplierInvoiceDAO {
	protected final Log logger = LogFactory.getLog(getClass());
	
	public  int insert(SupplierInvoice si)throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
		     tx = session.beginTransaction();
		     session.saveOrUpdate(si);
		     session.getTransaction().commit();
		     return 0;
		}
		catch (Exception e) {
		     if (tx!=null) tx.rollback();
		     throw e;
		}
		finally {
		     session.close();
		}
	}
	public  int update(SupplierInvoice si) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
		     tx = session.beginTransaction();
		     session.saveOrUpdate(si);
		     tx.commit();
		     return 0;
		}
		catch (Exception e) {
		     if (tx!=null) tx.rollback();
		     throw e;
		}
		finally {
		     session.close();
		}
	}
	public  int delete(SupplierInvoice si) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
		     tx = session.beginTransaction();
		     session.delete(si);
		     tx.commit();
		     return 0;
		}
		catch (Exception e) {
		     if (tx!=null) tx.rollback();
		     throw e;
		}
		finally {
		     session.close();
		}
	}
	public  List<SupplierInvoice> getParts(int busid) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
		     List<SupplierInvoice> si=null;
		     String qrystr="from SupplierInvoice where business_id=:busid";
		     Query qry=session.createQuery(qrystr);
		     qry.setInteger("busid", busid);
		     Object o=qry.list();
		     si=(List<SupplierInvoice>)o;
		     return si;
		}
		catch (Exception e) {
		     throw e;
		}
		finally {
		     session.close();
		}
	}
	public  List<SupplierInvoice> getAllSuppCopys(int busid) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
		     List<SupplierInvoice> si=null;
		     String qrystr="from SupplierInvoice where business_id=:busid order by date";
		     Query qry=session.createQuery(qrystr);
		     qry.setInteger("busid", busid);
		     Object o=qry.list();
		     si=(List<SupplierInvoice>)o;
		     return si;
		}
		catch (Exception e) {
		     throw e;
		}
		finally {
		     session.close();
		}
	}
	public  SupplierInvoice getsupInvoiceById(int id) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
		     SupplierInvoice si=null;
		     String qrystr="from SupplierInvoice where id=:id";
		     Query qry=session.createQuery(qrystr);
		     qry.setInteger("id",id);
		     Object o=qry.uniqueResult();
		     si=(SupplierInvoice)o;
		     return si;
		}
		catch (Exception e) {
		     throw e;
		}
		finally {
		     session.close();
		}
	}
	
	public  List<SupplierInvoice> getAllSuppByNo(String scopyno,int busid) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
		     List<SupplierInvoice> si=null;
		     String qrystr="from SupplierInvoice where  invoice_num like '%"+scopyno+"%') and business_id=:busid order by date";
		     Query qry=session.createQuery(qrystr);
		     qry.setInteger("busid", busid);
		     Object o=qry.list();
		     si=(List<SupplierInvoice>)o;
		     return si;
		}
		catch (Exception e) {
		     throw e;
		}
		finally {
		     session.close();
		}
	}
}
